package cn.jaye.fabric.controller.rest;

import cn.jaye.fabric.application.GuessApplicationService;
import cn.jaye.fabric.application.TestApplicationService;
import cn.jaye.fabric.application.command.GuessCommand;
import cn.jaye.fabric.application.command.TestCommand;
import cn.jaye.fabric.common.constant.FabricConstant;
import cn.jaye.fabric.common.util.ResultUtil;
import cn.jaye.fabric.domain.service.BlockChainService;
import cn.jaye.fabric.infrastructure.mybatis.impl.UseRepositoryImpl;
import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Jaye
 * @create 2018-05-31 15:14
 */
@RestController
@RequestMapping("rest")
public class TestRestController {

    @Autowired
    private FabricConstant fabricConstant;

    @Autowired
    private UseRepositoryImpl useRepository;

    @Autowired
    private GuessApplicationService guessApplicationService;

    @Autowired
    private BlockChainService blockChainService;

    @Autowired
    private TestApplicationService testApplicationService;



    @PostMapping("test")
    public String list(HttpServletRequest request, TestCommand testCommand) {
        return JSON.toJSONString("");
    }

    @PostMapping("guesstest")
    public String guess(HttpServletRequest request, @RequestBody GuessCommand guessCommand) {
//        guessApplicationService.guess(guessCommand);
        return ResultUtil.success();
    }

    @PostMapping("list")
    public String list(HttpServletRequest request) {
        return ResultUtil.success(guessApplicationService.listScore());
    }

    @PostMapping("/match/list")
    public String listMatch(HttpServletRequest request) {
        return ResultUtil.success(guessApplicationService.listGuessMatch());
    }

    @PostMapping("regist")
    public String block(HttpServletRequest request) throws InterruptedException {
        testApplicationService.registUser();
        return "ok";
    }

    @PostMapping("guess")
    public String guess(HttpServletRequest request) {
        testApplicationService.guessGame();
        return null;
    }

    @PostMapping("myscore")
    public String myscore(HttpServletRequest request) {
        return ResultUtil.success(useRepository.getMyScore("123"));
    }

    @PostMapping("listScore")
    public String listScore(HttpServletRequest request) {
        return ResultUtil.success(useRepository.listScore());
    }

    @PostMapping("list/score/date")
    public String listScoreByDate(HttpServletRequest request) throws ParseException {
//        Date startDate = new Date();
//        Date endDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        Date today=new Date("2018-06-17 00:00:99");
        Date today = sdf.parse("2018-06-16 00:00:45");
        Date yestToday = DateUtils.addDays(today, -1);
        String endDate = sdf.format(today);
        String startDate = sdf.format(yestToday);
        return ResultUtil.success(useRepository.listScoreCountByDate(startDate, endDate));
    }
    @PostMapping("list/allow")
    public String listAllow(HttpServletRequest request) {
        return ResultUtil.success(guessApplicationService.listGuessAllowMatch());
    }



}
